package org.mspring.mlog.web.admin;

import org.mspring.mlog.common.exception.OperateException;
import org.mspring.mlog.web.BaseAdminController;
import org.mspring.mlog.entity.CommentEntity;
import org.mspring.mlog.common.constants.CommentStatus;
import org.mspring.mlog.service.CommentEntityService;
import org.mspring.mlog.support.TabItem;
import org.mspring.nbee.common.PageResult;
import org.mspring.nbee.db.dao.sql.builder.part.condition.Operator;
import org.mspring.nbee.db.dao.sql.builder.part.condition.SqlCondition;
import org.mspring.nbee.db.dao.sql.builder.part.condition.impl.SimpleCondition;
import org.mspring.nbee.db.dao.sql.pager.OrderType;
import org.mspring.nbee.db.dao.sql.pager.SqlPager;
import org.mspring.nbee.web.support.QueryParam;
import org.mspring.nbee.web.support.WebContext;
import org.mspring.nbee.web.view.json.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author Gao Youbo
 * @since 2015-01-03 14:41
 */
@Controller
@RequestMapping("/admin/comment")
@TabItem(item = "100120")
public class CommentController extends BaseAdminController {
    private CommentEntityService commentEntityService;

    @RequestMapping("/list")
    @TabItem(item = "100120005")
    public String list(WebContext context, Model model) {
        SqlPager pager = context.getPager();
        pager.addOrder("id", OrderType.DESC);
        List<SqlCondition> conditions = context.buildSqlConditions(model,
                new QueryParam(CommentEntity.Fields.content, Operator.LIKE),
                new QueryParam(CommentEntity.Fields.status, Operator.EQ)
        );
        conditions.add(new SimpleCondition("status", Operator.EQ, CommentStatus.APPROVED));
        PageResult<CommentEntity> result = commentEntityService.list(conditions, pager);
        model.addAttribute("result", result);
        return "/admin/comment/list";
    }


    @RequestMapping("/edit")
    @TabItem(item = "100120015")
    public String edit(WebContext context, Model model) {
        Long id = context.getParamAsLong("id");
        if (id == null) {
            throw new OperateException("操作异常：请选择要编辑的评论");
        }
        CommentEntity comment = commentEntityService.findById(id);
        model.addAttribute("comment", comment);
        return "/admin/comment/edit";
    }


    @RequestMapping("/edit/do")
    public String doEdit(WebContext context, Model model) {
        long id = context.getRequiredLong("id");
        CommentEntity comment = commentEntityService.findById(id);
        comment = context.fromRequest(comment);
        commentEntityService.update(comment);
        return "redirect:/admin/comment/list";
    }

    @RequestMapping("/delete/list")
    @TabItem(item = "100120010")
    public String listDelete(WebContext context, Model model) {
        SqlPager pager = context.getPager();
        pager.addOrder("id", OrderType.DESC);
        List<SqlCondition> conditions = context.buildSqlConditions(model,
                new QueryParam(CommentEntity.Fields.content, Operator.LIKE),
                new QueryParam(CommentEntity.Fields.status, Operator.EQ)
        );
        conditions.add(new SimpleCondition("status", Operator.EQ, CommentStatus.DELEtED));
        PageResult<CommentEntity> result = commentEntityService.list(conditions, pager);
        model.addAttribute("result", result);
        return "/admin/comment/deletelist";
    }

    @RequestMapping("/delete/do")
    @ResponseBody
    public JsonResult doDelete(WebContext context, Model model) {
        long id = context.getRequiredLong("id");
        boolean flag = commentEntityService.deleteById(id);
        return JsonResult.getJsonResult(flag);
    }

    /**
     * 标记为垃圾
     */
    @RequestMapping("/spam/list")
    @TabItem(item = "100120015")
    public String listSpam(WebContext context, Model model) {
        SqlPager pager = context.getPager();
        pager.addOrder("id", OrderType.DESC);
        List<SqlCondition> conditions = context.buildSqlConditions(model,
                new QueryParam(CommentEntity.Fields.content, Operator.LIKE),
                new QueryParam(CommentEntity.Fields.status, Operator.EQ)
        );
        conditions.add(new SimpleCondition("status", Operator.EQ, CommentStatus.SPAM));
        PageResult<CommentEntity> result = commentEntityService.list(conditions, pager);
        model.addAttribute("result", result);
        return "/admin/comment/spamlist";
    }

    /**
     * 标记为垃圾
     */
    @RequestMapping("/spam/do")
    @ResponseBody
    public JsonResult doSpam(WebContext context, Model model) {
        long id = context.getRequiredLong("id");
        boolean flag = commentEntityService.spam(id);
        return JsonResult.getJsonResult(flag);
    }

    /**
     * 恢复正常
     *
     * @param context
     * @param model
     */
    @RequestMapping("/recover/do")
    @ResponseBody
    public JsonResult doRecover(WebContext context, Model model) {
        long id = context.getRequiredLong("id");
        boolean flag = commentEntityService.recover(id);
        return JsonResult.getJsonResult(flag);
    }

    @Autowired
    public void setCommentEntityService(CommentEntityService commentEntityService) {
        this.commentEntityService = commentEntityService;
    }
}
